<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
					<jk-button
						type="warning"
						:loading="exportLoading"
						icon="el-icon-upload2"
						size="small"
						class="item-gap"
						@click="onExportExcel"
					>导出</jk-button>
				</div>
				<div class="flex-end-center-wrap">
					<jk-date-picker v-model="searchParams.belongDate" placeholder="日期" :picker-options="pickerOptions" size="small" style="width: 140px;" class="margin-5-0 item-gap" clearable />
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>

		<jk-card>
			<jk-table
				:loading="tableLoading"
				border
				:stripe="false"
				:data="tableData"
				:height="tableHeight + 30"
			>
				<vxe-column
					v-for="(item, index) in tableColumnList"
					:key="index"
					:title="item.title"
					:align="item.align"
					:field="item.keyName"
					:sortable="item.sortable"
					:fixed="item.fixed ? item.fixed : ''"
					:min-width="item.minWidth"
					:width="item.width"
				>
					<template slot-scope="scope">
						<span v-if="item.keyName === 'machineEfficiencyWan'">{{ scope.row.machineEfficiencyWan ? scope.row.machineEfficiencyWan + '%' : 0 }}</span>
						<span v-else-if="item.keyName === 'machineEfficiencyZao'">{{ scope.row.machineEfficiencyZao ? scope.row.machineEfficiencyZao + '%' : 0 }}</span>
						<span v-else-if="item.keyName === 'machineEfficiency'">{{ scope.row.machineEfficiency ? scope.row.machineEfficiency + '%' : 0 }}</span>
						<span v-else>{{ scope.row[item.keyName] }}</span>
					</template>
				</vxe-column>
			</jk-table>
		</jk-card>
	</div>
</template>

<script>
    import calHeight from '/src/mixins/cal-table-height';
    import { pageConfig, formatMonth, formatDate, tipsBar } from '/src/utils/jk/jk-tool';
    import getMenuColumn from '../../../mixins/getMenuColumn';
    import handlerCache from '../../../mixins/handler-cache';
    import { systemUserColumnConfigGet } from '@/api/system/column-config';
    import {
        productionMachineShiftOutputGetProductDayData
    } from '../../../api/production/frontGroupYieldStatistics';
    export default {
        name: 'VarietyAnalySestatement',
        components: {},
        mixins: [calHeight, getMenuColumn, handlerCache],
        data() {
            return {
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() > Date.now();
                    }
                },
                apiItem: {
                    'page': productionMachineShiftOutputGetProductDayData
                },
                totalData: {
                    totalOutputM: 0,
                    totalOutputKg: 0
                },
                tableColumnList: [],
                processList: [],
                tableData: [],
                dataId: null,
                workshopList: [],
                tableLoading: true,
                exportLoading: false,
                count: 0,
                searchParams: {
                    belongDate: ''
                }
            };
        },
        mounted() {
            this.getDependentData();
        },
        methods: {
            /** 导出按钮操作 */
            onExportExcel() {
                this.exportLoading = true;
                const params = JSON.parse(JSON.stringify(this.searchParams));
                this.$modal.confirm('是否确认导出所有数据数据项?').then(() => {
                    this.exportLoading = true;
                    return productionMachineShiftOutputGetProductDayData(params);
                }).then(res => {
                    if (res.data.status === 200) {
            import('@/vendor/Export2Excel').then(excel => {
              const tHeader = this.tableColumnList.map(x => x.title);
              const filterVal = this.tableColumnList.map(x => x.keyName);
              const list = res.data.res ? res.data.res : [];
              const data = this.formatJson(filterVal, list);
              excel.export_json_to_excel({
                header: tHeader,
                data,
                filename: `${this.$route.meta.title}(${formatDate(Date.now())})`,
                autoWidth: true,
                bookType: 'xlsx'
              });
            });
                    }
                    this.exportLoading = false;
                });
            },
            // Math.ceil(number);
            formatJson(filterVal, jsonData) {
                return jsonData.map(v => filterVal.map(j => {
                    return v[j];
                }));
            },
            getMenuColumnListRequest() {
                return systemUserColumnConfigGet({ path: 'VarietyAnalySestatement', filterShow: true }).then(res => {
                    if (res.data.status === 200) {
                        this.tableColumnList = res.data.res.columnConfigList && res.data.res.columnConfigList.length ? res.data.res.columnConfigList[0].itemList : [];
                    }
                });
            },
            onPageChange(pageIndex, pageSize) {
                this.searchParams.pageNo = pageIndex;
                this.searchParams.pageSize = pageSize;
                this.getListRequest();
            },
            clearCacheMethod() {
                // Object.assign(this.$data, this.$options.data());
                this.$nextTick(() => {
                    this.getDependentData();
                });
            },
            async getDependentData() {
                this.tableLoading = true;
                this.$set(this.searchParams, 'belongDate', formatDate(Date.now()));
                await this.getListRequest();
                await this.getMenuColumnListRequest();
                this.tableLoading = false;
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                const params = JSON.parse(JSON.stringify(this.searchParams));
                // if (params.belongDate) {
                //     params.startDate = params.belongDate[0];
                //     params.endDate = params.belongDate[1];
                // }
                return this.apiItem['page'](params).then(res => {
                    this.tableLoading = false;
                    this.count = res.data.res.count;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res ? res.data.res.map(x => {
                            return x;
                        }) : [];
                        this.tableData = res.data.res;
                    }
                });
            },
            /** 搜索按钮操作 */
            onSearchBtn() {
                this.searchParams.pageNo = 1;
                this.searchParams.count = 1;
                this.getListRequest();
            }
        }
    };
</script>
<style>
.machine-group-item {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border-left: solid 1px #eee;
  border-top: solid 1px #eee;
  width: 80px;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 4px;
  flex-shrink: 0;
}
</style>
